using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace MicroMsg.UI.Page
{
	public class SelectStatePage : BasePage
	{
		public class Group<T> : List<T>
		{
			public string Key
			{
				get;
				set;
			}
			public bool HasItems
			{
				get
				{
					return 0 != base.get_Count();
				}
				set
				{
				}
			}
			public Group(string name, IEnumerable<T> items)
			{
				this.Key = name;
				using (IEnumerator<T> enumerator = items.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						T current = enumerator.get_Current();
						base.Add(current);
					}
				}
			}
			public override bool Equals(object value)
			{
				SelectStatePage.Group<T> group = value as SelectStatePage.Group<T>;
				return group != null && this.Key.Equals(group.Key);
			}
			public override int GetHashCode()
			{
				return base.GetHashCode();
			}
		}
		public static string sStrSelectedState = null;
		private static List<SelectStatePage.Group<string>> statesData;
		private bool isFoundInList;
		private object mGroupHeaderLocker = new object();
		public bool sBIsSelectorShown;
		private ScrollViewer mScrollViewer;
		internal Grid LayoutRoot;
		internal StackPanel ContentPanel;
		internal panelTitleControl panel1Line;
		internal LongListSelector mListGroupAreas;
		private bool _contentLoaded;
		public SelectStatePage()
		{
			this.InitializeComponent();
			SelectStatePage.sStrSelectedState = null;
			base.add_Loaded(new RoutedEventHandler(this.onPageLoaded));
		}
		private void onPageLoaded(object sender, RoutedEventArgs e)
		{
			if (SelectStatePage.sStrSelectedState != null && SelectStatePage.sStrSelectedState.get_Length() > 0 && this.isFoundInList)
			{
				this.mListGroupAreas.ScrollTo(SelectStatePage.sStrSelectedState);
			}
			Canvas.SetLeft(this.mListGroupAreas, 0.0);
		}
		protected override void OnPageIn(object[] object_0)
		{
			SelectStatePage.sStrSelectedState = AccountMgr.getCurAccount().strCity;
			if (object_0 != null && object_0[0] is List<SelectStatePage.Group<string>>)
			{
				SelectStatePage.statesData = (object_0[0] as List<SelectStatePage.Group<string>>);
				using (List<SelectStatePage.Group<string>>.Enumerator enumerator = SelectStatePage.statesData.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SelectStatePage.Group<string> current = enumerator.get_Current();
						using (List<string>.Enumerator enumerator2 = current.GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								string current2 = enumerator2.get_Current();
								if (current2 == SelectStatePage.sStrSelectedState)
								{
									this.isFoundInList = true;
									break;
								}
							}
						}
						if (this.isFoundInList)
						{
							break;
						}
					}
				}
				if (this.mListGroupAreas.ItemsSource == null)
				{
					this.mListGroupAreas.ItemsSource = SelectStatePage.statesData;
				}
			}
		}
		protected override void OnPageOut()
		{
		}
		private void PhoneCountry_ItemClick(object sender, object object_0)
		{
			string text = object_0 as string;
			if (text != null)
			{
				SelectStatePage.saveAreaData(text);
			}
			SelectCountryPage.shoudCloseOnBack = true;
			base.GoBack();
		}
		private static void saveAreaData(string strCity)
		{
			SelfPersonCard selfPersonCard = AccountMgr.getSelfPersonCard();
			if (selfPersonCard != null)
			{
				if (strCity != null)
				{
					selfPersonCard.strCity = SelectStatePage.trimToEmpty(strCity);
				}
				AccountMgr.setSelfPersonCard(selfPersonCard);
				Contact contact = StorageMgr.contact.method_0(AccountMgr.curUserName);
				if (contact != null)
				{
					contact.strCity = selfPersonCard.strCity;
					contact.strProvince = selfPersonCard.strProvince;
					StorageMgr.contact.modify(contact);
				}
			}
		}
		private static string trimToEmpty(string string_0)
		{
			if (string_0 != null)
			{
				return string_0.Trim().Replace("\n", "");
			}
			return "";
		}
		private void GroupHeaderClick(object sender, object object_0)
		{
			this.ShownSelector();
		}
		public void ShownSelector()
		{
			lock (this.mGroupHeaderLocker)
			{
				if (this.sBIsSelectorShown)
				{
					return;
				}
				this.sBIsSelectorShown = true;
			}
			this.mListGroupAreas.DisplayGroupView();
		}
		private void mListGroupAreas_GroupViewOpened(object sender, GroupViewOpenedEventArgs e)
		{
			QuadraticEase quadraticEase = new QuadraticEase();
			quadraticEase.set_EasingMode(0);
			IEasingFunction easingFunction = quadraticEase;
			Storyboard storyboard = new Storyboard();
			ItemsControl itemsControl = e.ItemsControl;
			using (IEnumerator<object> enumerator = itemsControl.get_Items().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					object current = enumerator.get_Current();
					UIElement uIElement = itemsControl.get_ItemContainerGenerator().ContainerFromItem(current) as UIElement;
					if (uIElement != null)
					{
						Border border = VisualTreeHelper.GetChild(uIElement, 0) as Border;
						if (border != null)
						{
							DoubleAnimationUsingKeyFrames doubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();
							EasingDoubleKeyFrame easingDoubleKeyFrame = new EasingDoubleKeyFrame();
							easingDoubleKeyFrame.set_KeyTime(TimeSpan.FromMilliseconds(0.0));
							easingDoubleKeyFrame.set_Value(-90.0);
							easingDoubleKeyFrame.set_EasingFunction(easingFunction);
							EasingDoubleKeyFrame easingDoubleKeyFrame2 = new EasingDoubleKeyFrame();
							easingDoubleKeyFrame2.set_KeyTime(TimeSpan.FromMilliseconds(500.0));
							easingDoubleKeyFrame2.set_Value(0.0);
							easingDoubleKeyFrame2.set_EasingFunction(easingFunction);
							doubleAnimationUsingKeyFrames.get_KeyFrames().Add(easingDoubleKeyFrame);
							doubleAnimationUsingKeyFrames.get_KeyFrames().Add(easingDoubleKeyFrame2);
							Storyboard.SetTargetProperty(doubleAnimationUsingKeyFrames, new PropertyPath(PlaneProjection.RotationXProperty));
							Storyboard.SetTarget(doubleAnimationUsingKeyFrames, border.get_Projection());
							storyboard.get_Children().Add(doubleAnimationUsingKeyFrames);
						}
					}
				}
			}
			storyboard.Begin();
		}
		private void mListGroupAreas_GroupViewClosing(object sender, GroupViewClosingEventArgs e)
		{
			e.Cancel = true;
			if (this.mListGroupAreas == null)
			{
				return;
			}
			this.mListGroupAreas.CloseGroupView();
			if (e.SelectedGroup != null)
			{
				if (this.mScrollViewer == null)
				{
					this.mListGroupAreas.ScrollToGroup(e.SelectedGroup);
				}
				else
				{
					SelectStatePage.Group<string> group = e.SelectedGroup as SelectStatePage.Group<string>;
					double num = 0.0;
					using (List<SelectStatePage.Group<string>>.Enumerator enumerator = SelectStatePage.statesData.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							SelectStatePage.Group<string> current = enumerator.get_Current();
							if (current.Key == group.Key)
							{
								break;
							}
							if (current.get_Count() != 0)
							{
								num += (double)(current.get_Count() + 1);
							}
						}
					}
					this.mScrollViewer.ScrollToVerticalOffset(num);
				}
			}
			this.sBIsSelectorShown = false;
		}
		private void regEventHandForList()
		{
			if (this.mScrollViewer != null)
			{
				return;
			}
			ScrollViewer scrollViewer = UIElementTraveler.FindElementRecursive(this.mListGroupAreas, typeof(ScrollViewer)) as ScrollViewer;
			this.mScrollViewer = scrollViewer;
			this.mListGroupAreas.ScrollingCompleted += new EventHandler(this.mListGroupAreas_ScrollingCompleted);
		}
		private void mListGroupAreas_ScrollingCompleted(object sender, EventArgs e)
		{
			if (this.mScrollViewer != null)
			{
				this.mScrollViewer.ScrollToVerticalOffset(this.mScrollViewer.get_VerticalOffset());
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Setting/SelectStatePage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.ContentPanel = (StackPanel)base.FindName("ContentPanel");
			this.panel1Line = (panelTitleControl)base.FindName("panel1Line");
			this.mListGroupAreas = (LongListSelector)base.FindName("mListGroupAreas");
		}
	}
}
